package com.source.hotelbath.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ffcs.network.imgdisplayer.ImgLoader;
import com.source.hotelbath.R;
import com.source.hotelbath.activity.LinenListActivity;
import com.source.hotelbath.base.CustomerAdapter;
import com.source.hotelbath.model.GoodsListBean;
import com.source.hotelbath.utils.ToastUtils;
import com.source.hotelbath.utils.ToolsUtil;
import com.source.hotelbath.utils.dialog.CustomerPromptDlg;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhouMeng on 2017/9/24.
 */

public class LinenItemAdapter extends CustomerAdapter {

    private int ADD=0;
    private int PICITEM=1;
    private Context mContext;
    private List<GoodsListBean> commonGood =new ArrayList<>();
    private Handler handler;
    private List<GoodsListBean> commonlist=new ArrayList<>();
    private int hotelId;
    public LinenItemAdapter(Context context, List<GoodsListBean> commonGood, Handler myHandler, List<GoodsListBean> commonlist) {

        this.mContext=context;
        this.commonGood=commonGood;
        handler=myHandler;
        this.commonlist=commonlist;

    }

    @Override
    public int getCount() {
        return commonGood.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (position==commonGood.size()){
            return ADD;
        }else {
            return PICITEM;
        }
    }
    @Override
    public int getViewTypeCount() {
        return 2;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position)==PICITEM){
            ViewHolder holder;
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.linen_item_pic, null);
                holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
                holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                holder.tv_pic = (ImageView) convertView.findViewById(R.id.tv_pic);
                holder.iv_delect= (ImageView) convertView.findViewById(R.id.iv_delect);
                holder.ll_click= (LinearLayout) convertView.findViewById(R.id.ll_click);
                holder.ll_delect= (LinearLayout) convertView.findViewById(R.id.ll_delect);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_num.setText(commonGood.get(position).getNum()+"");
            holder.tv_name.setText(commonGood.get(position).getGoodsName());
            holder.tv_price.setText("￥"+commonGood.get(position).getWashPrice());

//        holder.tvReads.setText(NewsHelper.getPersonNum(mNewsData.get(position).getViewCount()) + mContext.getString(R.string.person_reads));

                ImgLoader.load(commonGood.get(position).getGoodsImgpath())
                        .defRes(R.drawable.ic_def)
                        .errorRes(R.drawable.ic_def)
                        .width(300)
                        .height(300)
                        .into( holder.tv_pic);
            holder.ll_click.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(position);
                }
            });
            holder.ll_delect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteItem(position);
                }
            });

        }else {
            ViewHolder holder;
            if (null == convertView) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.linen_item_add, null);
                holder.iv_add = (ImageView) convertView.findViewById(R.id.iv_add);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(hotelId==0){
                        ToastUtils.showToast(mContext,"请选择酒店");
//                        Toast.makeText(mContext,"请选择酒店",Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(mContext,LinenListActivity.class);
                        intent.putExtra("hotelId",hotelId);
                        intent.putParcelableArrayListExtra("commonlist", (ArrayList<? extends Parcelable>) commonlist);
                        mContext.startActivity(intent);
                    }

                }
            });
        }



        return convertView;
    }

    private void deleteItem(int position) {

        for (int i=0;i<commonlist.size();i++){
            if (commonlist.get(i).getId()==commonGood.get(position).getId()){
                commonlist.get(i).setCheckType(0);
            }
        }
        commonGood.remove(position);
        notifyDataSetChanged();
    }

    private void showDialog(final int position) {

        CustomerPromptDlg dlg = new CustomerPromptDlg(mContext);
        dlg.setMsg(commonGood.get(position).getGoodsName()+"-数量");
        dlg.setListener(new CustomerPromptDlg.DlgListener() {
            @Override
            public void confirm(String s) {
                  int num=Integer.parseInt(s);
                 commonGood.get(position).setNum(num);
                 notifyDataSetChanged();
                handler.sendEmptyMessage(1);
            }

            @Override
            public void cancel() {

            }
        });
        dlg.show();
    }

    public void addData(int hotelId) {
        this.hotelId=hotelId;
    }

    class ViewHolder {
        TextView tv_num;
        TextView tv_name;
        TextView tv_price;
        ImageView iv_delect;
        LinearLayout ll_click;
        LinearLayout ll_delect;
        ImageView tv_pic;
        ImageView iv_add;

    }
}
